package edu.xidian.forum.controller;

import edu.xidian.forum.common.Result;
import edu.xidian.forum.dto.CommentDto;
import edu.xidian.forum.entity.Comment;
import edu.xidian.forum.entity.User;
import edu.xidian.forum.service.CommentService;
import edu.xidian.forum.service.UserService;
import edu.xidian.forum.vo.CommentVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static edu.xidian.forum.jwt.JwtUtil.USER_NAME;


@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;
    @Resource
    private UserService userService;

    @GetMapping("/get_comments")
    public Result<List<CommentVO>> getCommentByTopicID(@RequestParam(value = "topicid", defaultValue = "1") String topicid) {
        List<CommentVO> lstComment = commentService.getCommentsByTopicID(topicid);
        return Result.success(lstComment);
    }

    @PostMapping("/add_comment")
    public Result<Comment> add_comment(@RequestHeader(value = USER_NAME) String userName, @RequestBody CommentDto dto) {
        User user = userService.getUser(userName);
        Comment comment = commentService.create(dto, user);
        return Result.success(comment);
    }
}

